package servlet;

import dao.NoticeDao;
import dao.factoty.DaoFactory;
import domain.Notice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

@WebServlet(name = "AddNoticeServlet", value = "/AddNoticeServlet")
public class AddNoticeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        NoticeDao noticeDao = DaoFactory.getNoticeDaoInstance();
        String title = request.getParameter("title");
        String details = request.getParameter("details");
        Notice notice = new Notice();
        notice.setTitle(title);
        notice.setDetails(details);
        notice.setTime(new Date());
        try{
            noticeDao.addNotice(notice);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("/NoticeListServlet").forward(request, response);
    }
}
